home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / DIR.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  3KB  |  191 lines

  1. #include "jam.h"
  2. /*
  3.  * Name:    MG 2a
  4.  *        Directory management functions
  5.  * Created:    Ron Flax (ron@vsedev.vse.com)
  6.  *        Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987
  7.  */
  8.   
  9. #include "def.h"
  10. #include "keyname.h"
  11.   
  12. #ifdef FAT
  13. # include "direct.h"
  14. #else
  15. # include "dirent.h"
  16. # include "unistd.h"
  17. #endif
  18.  
  19. #include "malloc.h"
  20.   
  21. #ifndef NO_DIR
  22. #ifndef    getwd            /* may be a #define */
  23.   char    *getwd();
  24. #endif
  25. char    *wdir = 0;
  26. static char cwd[NFILEN];
  27.  
  28. static char *nocwd = "Can't get current directory, trying HOME!";
  29. static char *nohome = "Can't resolve HOME!";
  30.  
  31. #ifndef WINDOWED
  32. void setdir(s)
  33. char *s;
  34. {
  35.   chdir(s);
  36. }
  37. #endif
  38. /*
  39.  * Initialize anything the directory management routines need
  40.  */
  41. dirinit()
  42. {
  43.   char savename[MAXPATH];
  44.  
  45.   savename[0] = '\0';
  46.   if (cwd[0])
  47.     strcpy(savename, cwd);        /* have old valid dir? */
  48.   else
  49.     strcpy(savename, homedir);        /* revert to HOME if can */
  50.  
  51.   wdir = getwd(cwd);
  52.   if (!wdir)
  53.     {
  54.       ewprintf(nocwd);
  55.       if (savename[0] != '\0')
  56.     {
  57.       strcpy(cwd, savename);
  58.       wdir = cwd;
  59.     }
  60.       else
  61.     panic(nohome);
  62.     }
  63.   return TRUE;
  64. }
  65.  
  66. /* split from changedir (JAM)
  67.  */
  68. gotodir(bufc)
  69. char *bufc;
  70. {
  71.   char savename[MAXPATH];
  72.   char *temp = (char *)calloc(strlen(bufc) + 1, sizeof(char));
  73.   char *temp1 = (char *)calloc(strlen(wdir) + 1, sizeof(char));
  74.   BOOL s = FALSE;
  75.  
  76.   if (temp && temp1)
  77.     {
  78.       strcpy(temp, bufc);
  79.       strcpy(temp1, wdir);
  80.       adjustnamecase(temp);
  81.       adjustnamecase(temp1);
  82.       s = (strcmp(temp, temp1) == 0);
  83.     }
  84.   free(temp);
  85.   free(temp1);
  86.  
  87.   if (s)
  88.     return(TRUE);   /* already same place */
  89.   
  90.   strcpy(savename, cwd);        /* save old dir in case error */
  91.  
  92. #ifdef FAT
  93.   if (bufc[1] && (bufc[1] == ':')) /* drive spec */
  94.     setdisk(bufc[0]);
  95. #endif
  96.  
  97.   if (chdir(bufc) == -1) 
  98.     {
  99.       ewprintf("Can't change dir to %s", bufc);
  100.       return(FALSE);
  101.     } 
  102.   else 
  103.     {
  104.       if (!(wdir = getwd(cwd)))
  105.     {
  106.           ewprintf(nocwd);
  107.       strcpy(cwd, savename);
  108.           wdir = cwd;
  109.           return (FALSE);
  110.         }
  111.       return(TRUE);
  112.     }
  113. }
  114.  
  115. /* given a BUFFER, make the file's root directory
  116.  * the current directory. makes it easy to support
  117.  * visit-file, etc, to same dir
  118.  */
  119. #include "kbd.h"  /* sigh */
  120.  
  121. switchdir(bp)
  122. BUFFER *bp;
  123. {
  124.   char buf[NFILEN];
  125.   BOOL result = FALSE;
  126.   
  127.   if (bp->b_fname[0] && ((bp->b_flag & BFDIR) == 0))
  128.     {
  129.       dirfrombp(buf, bp);
  130.       result = gotodir(buf);
  131.       if (!result)
  132.     bp->b_flag |= BFDIR;       /* failure, don't try again */
  133.     }
  134.   return result;
  135. }
  136.  
  137. void dirfrombp(buf, bp)
  138. char *buf;
  139. BUFFER *bp;
  140. {
  141.   extern MAPS *name_mode();
  142.   register char *s;
  143.   
  144.   strcpy(buf, bp->b_fname);   /* whole name */
  145.   
  146.   /* strip name from normal 'file' buffers
  147.    */
  148.   s = &buf[strlen(buf)-1]; 
  149.   if (bp->b_modes[0] != name_mode(DiredStr))
  150.     for (; (*s && (s >= buf) && (*s != BDC1)); s--)
  151.       ;
  152.   if ((s >= buf) && (*s == BDC1))
  153.     *s = '\0';   
  154.   
  155.   /* root check, ick
  156.    */
  157.   for (s = buf; *s; s++)
  158.     if (*(s + 1) == '\0')
  159.       if (*s == ':')
  160.     {
  161.       *(s + 1) = BDC1;
  162.       *(s + 2) = '\0';
  163.       break;
  164.     }   
  165.  
  166.  if (strcmp(buf, bp->b_fname) == 0)  /* not computed, use cwd */
  167.    strcpy(buf, cwd);
  168. }
  169.  
  170. char *dirpath()
  171. {
  172.   static char buf[NFILEN+1];
  173.   register int i;
  174.   
  175.   strcpy(buf, wdir);
  176.   i = strlen(buf) - 1;
  177.   if (buf[i] != BDC1) 
  178.     {
  179.       i++;
  180.       buf[i++] = BDC1;
  181.       buf[i] = '\0';
  182.     }
  183.   adjustnamecase(buf);
  184.   return(buf);
  185. }
  186.  
  187. #endif
  188.  
  189.  
  190.  
  191.